package ${t.packagePath}.controller;

import java.io.Serializable;
import java.util.List;
import java.util.Arrays;
import java.util.stream.Collectors;
import java.util.ArrayList;
import java.util.Map;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import ${t.packagePath}.model.${t.permissionTableName};
import ${t.packagePath}.model.${t.userTableName};
import ${t.packagePath}.model.${t.userRoleTableName};
import ${t.packagePath}.model.${t.roleMenuTableName};
import ${t.packagePath}.model.${t.rolePermissionTableName};
import ${t.packagePath}.service.*;
import ${t.packagePath}.utils.AjaxJson;
import ${t.packagePath}.config.Stp${t.userTableName}Util;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;
import lombok.AllArgsConstructor;
import java.lang.reflect.Method;
import cn.hutool.core.util.ReflectUtil;
import cn.zhxu.bs.BeanSearcher;
import cn.zhxu.bs.SearchResult;
import cn.zhxu.bs.util.MapUtils;
import javax.servlet.http.HttpServletRequest;
import io.swagger.annotations.ApiOperation;
import ${t.packagePath}.service.${t.mkNameBig}Service;
import ${t.packagePath}.model.${t.mkNameBig};
<#if cfg.saTokenAuthWay == 1 >import cn.dev33.satoken.stp.StpUtil;
<#else>import cn.dev33.satoken.annotation.SaCheckPermission;
</#if>


/**
 * Controller: 角色权限
 * @author ${cfg.author} 
 */
@RestController
@AllArgsConstructor(onConstructor_ = {@Autowired})
public class ${t.rolePermissionTableName}Controller {

	${t.rolePermissionTableName}Service ${t.rolePermissionTableName?uncap_first}Service;
	${t.permissionTableName}Service ${t.permissionTableName?uncap_first}Service;

	/**
     * 拉取权限id列表  根据指定roleId
     *
     * @param roleId
     * @return
     */
    @RequestMapping("/SpRoleSpPermission/getPermissionsByRid")
	@SaCheckPermission(type= Stp${t.userTableName}Util.TYPE,value=${t.rolePermissionTableName}.PERMISSION_CODE)
    public AjaxJson getPcodeByRid(@RequestParam(defaultValue = "0") long roleId) {
        // 防止拉出全部
        if (roleId == 0) {
            return AjaxJson.getError("roleId不能为null或0");
        }
        //角色拥有的权限ids
        List<Long> permissionIds = ${t.rolePermissionTableName?uncap_first}Service.lambdaQuery()
			.eq(${t.rolePermissionTableName}::getRoleId, roleId)
			.list()
			.stream()
			.map(${t.rolePermissionTableName}::getPermissionId)
			.collect(Collectors.toList());
			return AjaxJson.getSuccessData(permissionIds);
        }


	/**
	* 修改指定角色的拥有的权限
	*
	* @param roleId 角色id
	* @param codes  拥有的权限码集合
	* @return
	*/
	@RequestMapping("/SpRoleSpPermission/updatePermissionsByRid")
	@SaCheckPermission(type= Stp${t.userTableName}Util.TYPE,value=${t.rolePermissionTableName}.PERMISSION_CODE)
	@Transactional(rollbackFor = Exception.class)
	public AjaxJson updatePcodeByRid(Long roleId, HttpServletRequest request) {
		Map<String, Object> flat = MapUtils.flat(request.getParameterMap());
		Object object = flat.get("ids");
		if (object == null) {
			return AjaxJson.getSuccess();
		}
		List<String> ids = Arrays.asList(object.toString().split(","));
		List<${t.permissionTableName}> list = ${t.permissionTableName?uncap_first}Service.lambdaQuery().in(${t.permissionTableName}::getId, ids).list();
		${t.rolePermissionTableName?uncap_first}Service.remove(new LambdaQueryWrapper<${t.rolePermissionTableName}>().eq(${t.rolePermissionTableName}::getRoleId, roleId));
		for (${t.permissionTableName} ${t.permissionTableName?uncap_first} : list) {
			${t.rolePermissionTableName} ${t.rolePermissionTableName?uncap_first} = new ${t.rolePermissionTableName}();
			${t.rolePermissionTableName?uncap_first}.setRoleId(roleId);
			${t.rolePermissionTableName?uncap_first}.setPermissionId(spPermission.getId());
			${t.rolePermissionTableName?uncap_first}Service.save(${t.rolePermissionTableName?uncap_first});
		}
		return AjaxJson.getSuccess();
	}
}
